package com.qire.manhua.controller;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.widget.EditText;

import com.qire.common.basic.StringUtil;
import com.qire.common.model.entity.UserEntity;
import com.qire.common.support.DialogFastBuildUtil;
import com.qire.common.support.DialogFastBuildUtil.Builder;
import com.qire.common.support.base.EventController;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.R;
import com.qire.manhua.activity.ComicCommentActivity;
import com.qire.manhua.activity.helper.LoginRouter;
import com.qire.manhua.model.viewModel.IComicCommentViewModel;
import com.qire.manhua.dal.service.ComicCommentService;

import net.yslibrary.android.keyboardvisibilityevent.util.UIUtil;

import androidx.annotation.NonNull;

public class ComicCommentController extends EventController<ComicCommentActivity> {

    private ComicCommentService comicCommentService = new ComicCommentService();

    private IComicCommentViewModel viewModel;

    public ComicCommentController(@NonNull ComicCommentActivity activity) {
        super(activity);
    }

    @Override
    protected void bindView() {
        owner.viewDataBinder().setComicCommentController(this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(IComicCommentViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    /**
     * 返回按钮单击事件
     * @param view
     */
    public void backBtnOnClick(View view) {
        owner.onBackPressed();
    }

    private Builder dialogBuilder;

    /**
     * 发表按钮单击事件
     * @param view
     */
    public void publishBtnOnClick(View view) {
        // 未登录先登录
        if(LoginRouter.ROUTER.checkLoginRouteIfNeed()) {
            return;
        }

        // 未设置昵称，先更新昵称
        if(!UserEntity.self.haveNickname()) {
            SpannableString title = new SpannableString("填写昵称\n（只允许中文、英文、数字）");
            title.setSpan(new AbsoluteSizeSpan(12, true), 5, title.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            dialogBuilder = DialogFastBuildUtil.builder(R.layout.dialog_common_edit_enter)
                    .setTextView(R.id.dialog_title, title)
                    .setTextHint(R.id.dialog_content, "请输入您的昵称")
                    .setTextView(R.id.dialog_button_dark, v -> dialogBuilder.dismiss())
                    .setTextView(R.id.dialog_button_light, this::changeNicknameBtnOnClick)
                    .show();
            return;
        }
        String content = viewModel.getContent().getValue();

        if(StringUtil.isEmpty(content)) {
            ToastHelper.showToastWithoutDelay("写点儿什么吧");
            return ;
        }

        if(content.length() < 10){
            ToastHelper.showSnackBar("评论不能少于10个字", Prompt.WARNING);
            return ;
        }

        if(content.length() > 800){
            ToastHelper.showSnackBar("请控制字数在800字以内", Prompt.WARNING);
            return ;
        }
        comicCommentService.submitComment(owner.comicId(), owner.chapterId(), content);
        UIUtil.hideKeyboard(owner);
    }

    public void changeNicknameBtnOnClick(View view) {
        EditText content = dialogBuilder.findView(R.id.dialog_content);
        String nameText = content.getText().toString();
        if (StringUtil.notEmpty(nameText)) {
            String tmpName = UserEntity.self.getName();
            UserEntity.self.setName(nameText);
            if(UserEntity.self.isIllegalName()) {
                UserEntity.self.setName(tmpName);
                ToastHelper.showToast("请输入合法的昵称，昵称中不能包含非法字符，只能由数字、字母、中文组成");
                return;
            }
            comicCommentService.updateNickname();
        }
        dialogBuilder.dismiss();
    }

}
